package edu.hue.jk.service;

import com.sun.org.apache.xpath.internal.operations.Mod;
import edu.hue.jk.Mapper.RoomDao;
import edu.hue.jk.Mapper.StudentDao;
import edu.hue.jk.entity.Room;
import edu.hue.jk.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private RoomDao roomDao;

    public String list(Model model){
        List<Student> studentList=studentDao.selectAll();
        model.addAttribute("studentList",studentList);
        return "studentlist";
    }
    public String add(Model model,Student student){
            String sid=student.getSid();
            if(studentDao.queryById(sid)!=null){
                model.addAttribute("msg","学生已存在");
                return "addstudent";
            }
            else{
                studentDao.insert(student);
                Room room=new Room();
                room.setDid(student.getDid());
                room.setBid(student.getBid());
                room.setSid(student.getSid());
                room.setName(student.getName());
                room.setSex(student.getSex());
                roomDao.insert(room);
                model.addAttribute("msg","添加成功");
                return "addstudent";
            }


    }

    public String delete(Model model,String sid){
        studentDao.deleteById(sid);
        roomDao.deleteById(sid);
        List<Student> studentList=studentDao.selectAll();
        model.addAttribute("studentList",studentList);
        return "studentlist";
    }

    public String update(Student student,Model model){
        studentDao.update(student);
        Room room=new Room();
        room.setDid(student.getDid());
        room.setBid(student.getBid());
        room.setSid(student.getSid());
        room.setName(student.getName());
        room.setSex(student.getSex());
        roomDao.update(room);
        model.addAttribute("msg","修改成功");
        return "updatestudent";
    }

    public String updates(String sid,Model model){
        Student student=studentDao.queryById(sid);
        model.addAttribute("student",student);
        return "updatestudent";
    }

    public String query_1(Model model,String sid){

        if(studentDao.queryById(sid)==null){
            model.addAttribute("msg","查无此人");
            return "querystudent_1";
        }else{
            Student student=studentDao.queryById(sid);
            model.addAttribute("student",student);
            return "querystudent";
        }
    }

    public String selectroom(){
        return "selectroom_1";
    }
}
